package com.fuxi.cloud.desreport.render.a.ar;



/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.fuxi.cloud.common.constant.ExpConstant;
import com.fuxi.cloud.common.vo.Result;
import com.fuxi.cloud.desreport.entity.JimuReport;
import com.fuxi.cloud.desreport.model.RenderInfo;
import com.fuxi.cloud.desreport.model.ReportDbInfo;
import com.fuxi.cloud.desreport.render.handler.CellsRenderHandler;
import com.fuxi.cloud.desreport.render.handler.RowRenderHandler;
import com.fuxi.cloud.desreport.render.handler.RowsRenderHandler;
import com.fuxi.cloud.desreport.render.utils.FreeMarkerUtils;
import com.fuxi.cloud.desreport.render.utils.RegexMatches;
import com.fuxi.cloud.desreport.render.utils.RenderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("baseRenderStrategy")
public class b extends a {
    private static final Logger g = LoggerFactory.getLogger(b.class);

    @Override
    public boolean support(RenderInfo renderInfo) {
        return a(b.class, renderInfo);
    }


    @Override
    public Result<JimuReport> render(RenderInfo renderInfo) {
        a();
        JSONObject jSONObject1 = a(renderInfo, this.e);
        JSONObject jSONObject2 = b(renderInfo, jSONObject1);
        jSONObject2.put("len", Integer.valueOf(jSONObject2.size() + 20));
        this.c.put("rows", jSONObject2);
        this.c.put("merges", renderInfo.getMerges());
        this.f.put("len", renderInfo.getLen());
        this.a.setJsonStr(this.c.toString());
        return Result.OK(this.a);
    }

    private void a() {
        HashMap var1 = new HashMap(5);
        HashMap var2 = new HashMap();
        Iterator var3 = this.b.entrySet().iterator();

        Object var6;
        Object var7;
        while(var3.hasNext()) {
            Map.Entry var4 = (Map.Entry)var3.next();
            String var5 = (String)var4.getKey();
            var6 = this.b.get(var5);
            var7 = this.b.get("expData");
            if (ObjectUtil.isNotEmpty(var7)) {
                Map var8 = (Map)var7;
                var2.putAll(var8);
            }

            if (var6 instanceof ReportDbInfo) {
                ReportDbInfo var14 = (ReportDbInfo)var6;
                List var9 = var14.getList();
                if (ObjectUtil.isNotEmpty(var9)) {
                    var1.put(var5, var9.get(0));
                } else {
                    var1.put(var5, new Object());
                }
            } else {
                var1.put(var5, var4.getValue());
            }
        }

        String var10 = this.e.toString();
        if (ObjectUtil.isNotEmpty(var2)) {
            Iterator var11 = var2.entrySet().iterator();

            while(var11.hasNext()) {
                Map.Entry var13 = (Map.Entry)var11.next();
                var6 = var13.getKey();
                var7 = var13.getValue();
                if (ObjectUtil.isNotEmpty(var7)) {
                    var10 = var10.replace(var6.toString(), var7.toString());
                } else {
                    var10 = var10.replace(var6.toString(), "");
                }
            }
        }

        var10 = var10.replaceAll("#\\{", "☯{");
        String var12 = FreeMarkerUtils.a(var10, var1);
        var12 = var12.replaceAll("☯\\{", "\\${");
        this.e = JSONObject.parseObject(var12, new Feature[]{Feature.OrderedField});
    }

    private JSONObject b(RenderInfo var1, JSONObject var2) {
        JSONObject var3 = new JSONObject(true);
        Integer var4 = 0;
        Integer var5 = 0;
        Integer var6 = 0;
        Object var7 = new HashMap(5);
        Boolean var8 = true;
        Integer var9 = 0;
        Iterator var10 = var2.entrySet().iterator();

        while(true) {
            JSONObject var13;
            Object var14;
            do {
                do {
                    Map.Entry var11;
                    Object var12;
                    do {
                        if (!var10.hasNext()) {
                            return var3;
                        }

                        var11 = (Map.Entry)var10.next();
                        var12 = var11.getValue();
                    } while(!(var12 instanceof JSONObject));

                    var13 = (JSONObject)var12;
                    var14 = var11.getKey();
                } while(ObjectUtil.isEmpty(var14));
            } while("-1".equals(var14));

            var6 = Integer.parseInt(var14.toString());
            JSONObject var15 = var13.getJSONObject("cells");
            if (var15 instanceof JSONObject) {
                Iterator var16 = var15.entrySet().iterator();

                while(var16.hasNext()) {
                    Map.Entry var17 = (Map.Entry)var16.next();
                    Object var18 = var17.getValue();
                    if (var18 instanceof JSONObject) {
                        String var19 = var17.getKey().toString();
                        JSONObject var20 = (JSONObject)var18;
                        String var21 = var20.getString("text");
                        if (ObjectUtil.isNotEmpty(var21)) {
                            Map var22 = this.a(var1, var13, var15, var19, var20, var21, (Map)var7);
                            if (ObjectUtil.isNotEmpty(var22)) {
                                var7 = var22;
                            }
                        }

                        var15.putIfAbsent(var19, var20);
                    }
                }

                var13.put("cells", var15);
            }

            if (var5 == 0) {
                var5 = var6;
            }

            Boolean var23 = false;
            JSONObject var24 = this.a(var1, var2, var13, var6, var5, var8);
            if (ObjectUtil.isNotEmpty(var24) && var24.size() > 0) {
                var3.putAll(var24);
                var23 = true;
                var4 = var6;
                var5 = var5 + var24.size();
                var8 = false;
            }

            if (!var23) {
                if (var4 > 0) {
                    var4 = 0;
                }

                if (var9 > 0) {
                    Integer var25 = var6 - var9;
                    if (var25 > 1) {
                        var5 = var5 + (var25 - 2);
                    }
                }

                if (var5 < var6) {
                    var5 = var6;
                }

                Integer finalVar = var5;
                var13 = RenderUtil.a(var13, (var1x, var2x) -> {
                    List var31 = RegexMatches.f(var2x);
                    if (ObjectUtil.isNotEmpty(var31) && var31.size() == 3) {
                        var2x = var2x.replace((CharSequence)var31.get(0), var31.get(0).toString() + ":" + var31.get(1).toString() + finalVar.toString());
                    }

                    return var2x;
                }, ExpConstant.FUNCTION);
                var3.put(var5.toString(), var13);
                var9 = var6;
                var5 = var5 + 1;
                if (var6 > var5) {
                    var5 = var6;
                }
            }
        }
    }

    public JSONObject a(RenderInfo paramRenderInfo, JSONObject paramJSONObject1, JSONObject paramJSONObject2, Integer paramInteger1, Integer paramInteger2, Boolean paramBoolean) {
        if (ObjectUtil.isNotEmpty(this.rowRenderHandlerMap))
            for (RowRenderHandler rowRenderHandler : this.rowRenderHandlerMap.values()) {
                if (rowRenderHandler.support(paramJSONObject2)) {
                    if (paramBoolean.booleanValue())
                        rowRenderHandler.beforeRenderRows(paramRenderInfo, paramJSONObject1);
                    JSONObject jSONObject1 = rowRenderHandler.beforeRenderRow(paramRenderInfo, paramJSONObject1, paramJSONObject2, paramInteger1);
                    if (ObjectUtil.isNotEmpty(jSONObject1))
                        paramJSONObject2 = jSONObject1;
                    JSONObject jSONObject2 = rowRenderHandler.render(paramRenderInfo, paramJSONObject1, paramJSONObject2, paramInteger1, paramInteger2);
                    if (ObjectUtil.isEmpty(jSONObject2)) {
                        JSONObject jSONObject = paramJSONObject2.getJSONObject("cells");
                        if (ObjectUtil.isNotEmpty(jSONObject))
                            for (Map.Entry entry : jSONObject.entrySet()) {
                                String str = entry.getKey().toString();
                                JSONObject jSONObject3 = (JSONObject)entry.getValue();
                                if (ObjectUtil.isNotEmpty(jSONObject3)) {
                                    String str1 = jSONObject3.getString("text");
                                    if (ObjectUtil.isNotEmpty(str1) && str1.indexOf("${") != -1)
                                        jSONObject3.put("text", "");
                                }
                            }
                    }
                    return jSONObject2;
                }
            }
        return null;
    }

    public Map a(RenderInfo paramRenderInfo, JSONObject paramJSONObject1, JSONObject paramJSONObject2, String paramString1, JSONObject paramJSONObject3, String paramString2, Map paramMap) {
        if (ObjectUtil.isNotEmpty(this.cellsRenderHandlerMap))
            for (CellsRenderHandler cellsRenderHandler : this.cellsRenderHandlerMap.values()) {
                if (cellsRenderHandler.support(paramJSONObject3, paramString2)) {
                    cellsRenderHandler.beforeRender(paramRenderInfo, paramJSONObject3, paramString2);
                    return cellsRenderHandler.render(paramRenderInfo, paramJSONObject1, paramJSONObject2, paramJSONObject3, paramString1, paramString2, paramMap);
                }
            }
        return null;
    }

    public JSONObject a(RenderInfo paramRenderInfo, JSONObject paramJSONObject) {
        if (ObjectUtil.isNotEmpty(this.rowsRenderHandlerMap))
            for (RowsRenderHandler rowsRenderHandler : this.rowsRenderHandlerMap.values()) {
                if (rowsRenderHandler.support(paramRenderInfo, paramJSONObject))
                    return rowsRenderHandler.render(paramRenderInfo, paramJSONObject);
            }
        return paramJSONObject;
    }
}
